// pages/base_mine/recycle_address/index.js

const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        address_list: [],
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

        // 验证登录
        app.verifyLogin();

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {
        // 获取列表
        app.execute(function() {
            let page = app.getPage();
            page.updateList(page);
        });
    },

    /**
     * 更新当前列表
    */
    updateList: function(page) {
        wx.showLoading({
            title: '更新列表...',
        });
        wx.request({
            url: app.data.service_url + 'recycle_address/listing',
            header: {
                'Client-Token': app.data.client_token
            },
            method: 'GET',
            success: function (response) {
                wx.hideLoading();
                if (200 === response.statusCode) {
                    if (0 === response.data.errno) {
                        let change = {
                            address_list: response.data.data.address_list
                        };
                        page.setData(change);
                    } else {
                        wx.showToast({
                            title: '更新列表失败,' + response.data.message,
                            icon: 'none'
                        })
                    };
                } else {
                    wx.showToast({
                        title: '更新列表失败，远程服务返回错误',
                        icon: 'none'
                    });
                };
            },
            fail: function () {
                wx.hideLoading();
                wx.showToast({
                    title: '更新列表失败，连接远程服务失败',
                    icon: 'none'
                });
            }
        });
    },

    /**
     * 显示单元菜单
    */
    showUnitMenu: function(e) {
        let data_id = e.currentTarget.dataset.id;
        let page = this;
        wx.showActionSheet({
            itemList: ['编辑', '删除'],
            itemColor: '#',
            success: function(e) {
                switch (e.tapIndex){
                    case 0 :
                        wx.navigateTo({
                            url: '/pages/base_mine/recycle_address/edit?data_id=' + data_id,
                        });
                        break;
                    case 1 :
                        page.delAddress(data_id);
                        break;
                };
            },
        });
    },

    /**
     * 地址删除
     */
    delAddress: function(data_id) {
        wx.showModal({
            title: '是否确认删除',
            content: '已提交的订单不受影响',
            showCancel: true,
            success: function(e) {
                if (true === e.confirm){
                    let data = {
                        id: data_id,
                    };
                    wx.showLoading({
                        title: '处理中...',
                    });
                    wx.request({
                        url: app.data.service_url + 'recycle_address/del',
                        header: {
                            'Client-Token': app.data.client_token
                        },
                        data: data,
                        method: 'POST',
                        success: function (response) {
                            wx.hideLoading();
                            if (200 === response.statusCode) {
                                if (0 === response.data.errno) {
                                    wx.showToast({
                                        title: '操作成功',
                                    });
                                    let page = app.getPage();
                                    page.onShow();
                                } else {
                                    wx.showToast({
                                        title: response.data.message,
                                        icon: 'none'
                                    });
                                };
                            } else {
                                wx.showToast({
                                    title: '远程服务返回错误',
                                    icon: 'none',
                                });
                            };
                        },
                        fail: function () {
                            wx.hideLoading();
                            wx.showToast({
                                title: '连接远程服务失败',
                                icon: 'none',
                            });
                        }
                    });
                };
            }
        })
    }

});